package web.components;

import java.io.IOException;

import javax.faces.component.FacesComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.component.UIGraphic;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

@FacesComponent("MapComponent")
public class MapComponent extends UIComponentBase {

	public MapComponent() {
		System.out.println("Map Component!!!");
		this.setRendered(true);
	}
	
	@Override
	public String getFamily() {
		return "Map";
	}
	
	@Override
	public void encodeBegin(FacesContext context) throws IOException { 
		MapComponent map = this;
		ResponseWriter writer = context.getResponseWriter();
		writer.startElement("div", map);
		writer.writeAttribute("id", "mapadiv", null);
		UIGraphic mapa = new UIGraphic();
		writer.startElement("img", mapa);
		writer.writeAttribute("src", "../MapImage", null);
		writer.writeAttribute("onclick", "getMouseXY(event)", null);
		writer.writeAttribute("id", "mapa", null);
		writer.endElement("img");
		writer.startElement("script", null);
		writer.writeAttribute("type", "text/javascript", null);
		writer.write("pins = [];");
	}
	
	@Override
	public void encodeEnd(FacesContext context) throws IOException {
		ResponseWriter writer = context.getResponseWriter();
		writer.write("$(document).ready(function() {makePins(pins);});");
		writer.endElement("script");
		writer.endElement("div");
	}

}